<?php
/**
 * Created by PhpStorm.
 * User: Lenovo
 * Date: 2019/4/26
 * Time: 13:53
 */

namespace app\admin\model;

use think\Db;
use think\Session;
class Withdrawal extends BaseModel
{
    protected $table='jc_withdrawal';
       public function tixianindex()
    	{
        $uid = Session::get('admin_uid');
        $vip = Guanli::where('id',$uid)->find();
        $arr = User::where('uid',$vip['uid'])->find();
        $rs=$this->alias('wd')
            ->join('jc_user us','wd.uid=us.uid')
            ->group('wd.wid')
            ->paginate(7);
        if($arr['daili']==1)
        {
            $rs = $this->alias('wd')
                ->join('jc_user us','wd.uid=us.uid')
                ->where('us.u_path','like',',%'.$arr['uid'].',%')
                ->group('wd.wid')
                ->paginate(5);
        }
        //提现的时候二维码的调用
       foreach ($rs as $k=>$v)
       {
           $qid=$v['qid'];
           $bid=$v['bid'];
           if($qid!=0)
           {
               $qid=Qrcode::where('qid',$qid)->find();
               $ztype=Zpaytype::where('zid',$qid['zid'])->find();
               $rs[$k]['ztype']=$ztype['z_type'];
               $rs[$k]['q_picture']=$qid['q_picture'];
           }
           else
           {
               $bid=Bankcard::where('bid',$bid)->find();
               $rs[$k]['ztype']='银行卡';
               $rs[$k]['q_picture']=$bid['b_card'];
               $rs[$k]['b_bank']=$bid['b_bank'];
           }
       }

        if(input('sub'))
        {
            $where=array();
            $cateid=input('cateid');//下拉菜单
            $key=input('Key');//输入框
            $datemin = input('datemin', '');//最小时间
            $datemax = input('datemax', '');//最大的吧
            if (isset($datemin) && $datemin != '') {
                $where['w_createtime']=['>=', $datemin . ' 00:00:00'];
                //dump($where);
            }
            if (isset($datemax) && $datemax != '') {
                $where['w_createtime']=['<=', $datemax . ' 23:59:59'];
            }

            if($cateid=='u_phone'){
                $where['u_phone']=['like', '%'.$key.'%'];
            }else if($cateid=='u_name'){
                $where['u_name']=['like', '%'.$key.'%'];
            }
            else if($cateid=='w_number'){
                $where['w_number']=['like', '%'.$key.'%'];
            }
            else if($cateid=='w_state'){
                if($key=='已完成')
                {
                    $key=3;
                }
                elseif ($key=='已退回')
                {
                    $key=2;
                }
                elseif ($key=="待打款")
                {
                    $key=1;
                    //echo $key;die;
                }
                $where['w_state']=['like', '%'.$key.'%'];
            }
                $rs=$this->alias('wd')
                    ->join('jc_user us','wd.uid=us.uid')
                    ->group('wd.wid');
            //dump($this->objectToarray($rs));die;
            $rs=$rs->where($where)->paginate(7,false,['query'=>request()->param()]);
            foreach ($rs as $k=>$v)
            {
                $qid=$v['qid'];
                $bid=$v['bid'];
                if($qid!=0)
                {
                    $qid=Qrcode::where('qid',$qid)->find();
                    $ztype=Zpaytype::where('zid',$qid['zid'])->find();
                    $rs[$k]['ztype']=$ztype['z_type'];
                    $rs[$k]['q_picture']=$qid['q_picture'];
                }
                else
                {
                    $bid=Bankcard::where('bid',$bid)->find();
                    $rs[$k]['ztype']='银行卡';
                    $rs[$k]['q_picture']=$bid['b_card'];
                    $rs[$k]['b_bank']=$bid['b_bank'];
                }
            }
            //dump($this->objectToarray($rs));die;
            return $rs;
        }
        else
        {
            return $rs;
        }
    }


    //提现到账的调用
    public function daozhang()
    {
        Db::startTrans();
        $wid=input('wid');
        $rs=$this->where('wid',$wid)->find();//查询uid,订单金额
        $yu=Yue::where('uid',$rs['uid'])->find();//查询余额
        $ar=array($yu['y_money'],-$rs['w_money']);
        $money=array_sum($ar);//求和函数进行求和
        //echo $money;die;
        $xiu=$this->where('uid',$rs['uid'])->update(['w_state'=>3]);//修改状态
        $dao=Yue::where('uid',$rs['uid'])->update(['y_money'=>$money]);
        if($xiu && $dao)
        {
            Db::commit();//提交事务
            return 1;
        }
        else
        {
            Db::rollback();// 回滚事务
            return 0;
        }
    }

    //提现拒绝原因
    public function tixianreason()
    {
        $data=input('post.');
        //print_r($data);die;
        $rs=$this->where('wid',$data['wid'])->update(['w_state'=>2,'w_reason'=>$data['w_reason']]);
        if ($rs!==false)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
}